//
//  ViewController.m
//  WKwebView的使用
//
//  Created by mingjun zhang on 2017/9/24.
//  Copyright © 2017年 mingjun zhang. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>

#define WIDTH   [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
#define WEBURL  @"https://www.tmall.com/?ali_trackid=2:mm_26632258_3504122_55934697:1490595491_210_1415363025&upsid=94bc012f013288b60bfe4be331d65ef0&clk1=94bc012f013288b60bfe4be331d65ef0"
//前者是加载状态的回调，后者是处理web界面的三种提示框(警告框、确认框、输入框)
@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic,strong) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"胜利的曙光";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:WEBURL]]];
    [self.view addSubview:_webView];
}


#pragma WKNavigationDelegate 加载状态的回调方法
//页面开始加载的时候调用
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"我开始加载了。。。。。。");
}

//当内容开始返回时调用
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
     NSLog(@"设置标题");
    self.navigationItem.title = @"胜利的曙光";
}

//界面加载完成时调用
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"我已经加载完成了。。。。。。");
}

//页面加载失败时调用
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
      NSLog(@"加载失败中。。。。。。");
}


- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    
}


#pragma WKNavigationDelegate 页面跳转的代理
//接到服务器跳转请求之后的调用
-(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"接收到服务器跳转之后调用");
}

//接收到响应后，决定是否跳转
-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSLog(@"接收到响应后，决定是否跳转");
    
      //如果是百度，则允许跳转
    if ([navigationResponse.response.URL.host.lowercaseString isEqualToString:@"www.tmall.com"]) {
        //允许跳转
        decisionHandler(WKNavigationResponsePolicyAllow);
        return;
    }
    //不允许跳转
    decisionHandler(WKNavigationResponsePolicyCancel);
    
}

//发送请求之前，决定是否跳转
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSLog(@"%@",navigationAction.request.URL.host.lowercaseString);
    
   //如果请求的事百度地址，则延迟5s以后跳转
    if ([navigationAction.request.URL.host.lowercaseString isEqualToString:@"www.tmall.com"]) {
                // 延迟5s之后跳转
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    // 允许跳转
//                    decisionHandler(WKNavigationActionPolicyAllow);
//                });
        
        // 允许跳转
        decisionHandler(WKNavigationActionPolicyAllow);
        return;
    }
    // 不允许跳转
    decisionHandler(WKNavigationActionPolicyCancel);
    
}



#pragma mark -- WKUIDelegate
/**
 *  web界面中有弹出警告框时调用
 *  @param webView           实现该代理的webview
 *  @param message           警告框中的内容
 *  @param frame             主窗口
 *  @param completionHandler 警告框消失调用
 */
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler{
    
}





-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.webView.UIDelegate = nil;
    self.webView.navigationDelegate = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end


